export const HOST = 'https://skp-wh.dingplayer.com/lottery/api/v1'
// export const HOST = 'http://192.168.1.80:8090/pa/api/v1'
const requestDateStr = {}

export const Request = ({ api, data = {}, method = 'get', header, controlToast = true }) => {
    requestDateStr[api] = new Date().getTime();
    const headers = header ? header : {};
    const app = getApp && getApp();
    if (app) {
        if(app.globalData.userInfo){
            headers.cookie =  app.globalData.token;
            data.uid = app.globalData.userInfo.id;
        }
    }
    headers['content-type'] = 'application/json; charset=utf-8';
    headers.from = 'mini_app';
    data.from = 'mini';
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${HOST}${api}`,
            header: headers,
            data: {
                ...data,
            },
            method: method,
            success (res) {
                console.log(res.cookies);
                if(res.cookies&&res.cookies.length>0){
                    app.globalData.token = res.cookies[0];
                }
                wx.hideLoading();    
                switch(res.data.code){
                  case 0:
                    resolve(res.data.data);
                    break;
                  case 2010:
                    wx.showToast({
                      title: res.data.msg,
                      icon: 'none',
                  }); 
                    app.globalData.userData = null;
                    app.globalData.token = null;
                    app.globalData.userInfo = null;
                    app.globalData.isLogin = false;
                    wx.clearStorage('globalData');
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                    break;
                  default:
                    if (res.data && res.data.msg) {
                      controlToast && wx.showToast({
                          title: res.data.msg,
                          icon: 'none',
                      }); 
                    } else {
                        controlToast && wx.showToast({
                            title: '未知错误',
                            icon: 'none',
                        });
                    }
                    reject(res.data)
                    break;
                }
            },
            fail (error) {
                console.log(error);
                wx.showToast({
                  title: '请求错误',
                  icon: 'none',
              })
            },
            complete (response) {
                if(response['header']['Set-Cookie']){
                    app.globalData.token = response['header']['Set-Cookie'];
                }
                if(response.statusCode==401){
                    console.log(response.data.msg);
                    try {
                        app.globalData.userData = {};
                        app.globalData.token = '';
                        app.globalData.userInfo = {};
                        app.globalData.isLogin = false;
                    } catch (e) { 

                    }
                    wx.showToast({
                        title: '未登录或登录超时，请重新登录',
                        icon: 'none',
                    }); 
                    wx.clearStorage('globalData');
                    wx.navigateTo({
                      url: '/pages/login/index',
                    })
                }
             }
        });
    })
};

export const UploadFiles = (tempFilePaths) => {
    const app = getApp && getApp();
    const header = {};
    const data = {};
    if (app) {
        header.sid = app.globalData.token;
        data.uid = app.globalData.userInfo.id;
    }
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            url: HOST + '/api/v1/upload', //仅为示例，非真实的接口地址
            filePath: tempFilePaths,
            name: 'file',
            header: header,
            formData: data,
            success: (res) => {
                try {
                    resolve(JSON.parse(res.data).data)
                } catch (e) {
                    reject()
                }
                //do something
            },
            fail: (error) => {
                reject(error)
            }
        })
    })
}